//
//  WYDetailVC.m
//  Brand
//
//  Created by Faker on 4/30/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "WYDetailVC.h"
#import "CollectionFixedHeaderLayout.h"
#import "WYView.h"
#import "Header.h"

#define CellIdentifer     @"CellIdentifer"

@interface WYDetailVC ()
@property (weak, nonatomic) IBOutlet UIView *navTopContentView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (strong, nonatomic) UICollectionView* myCollectionView;



@end

@implementation WYDetailVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomethingWhenAppDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
        // Custom initialization
    }
    return self;
}

-(void)doSomethingWhenAppDidBecomeActive{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.currentItemIndex inSection:0];
    UICollectionViewCell *cell = [self.myCollectionView cellForItemAtIndexPath:indexPath];
    WYView* mView = (WYView*)[cell viewWithTag:200];
    [mView startAnimation];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self drawNavTopContentView:self.navTopContentView];
    self.titleLabel.text = [[self.dataArray objectAtIndex:self.currentItemIndex] objectForKey:@"title"];
    
    [self setUp];
}

-(void)setUp
{
    
    CGRect frame = CGRectMake(0, self.navTopContentView.bottom,320,self.view.height-self.navTopContentView.bottom);
    self.myCollectionView = [self drawCollectionViewWithFrame:frame andFlowLayout:[self getFlowLayout]];
    [self registerCellAndHeaderForCollectionView:self.myCollectionView];
    self.myCollectionView.contentOffsetLeft = self.view.width*self.currentItemIndex;
    [self.view addSubview:self.myCollectionView];

}

#pragma mark - CollectionView methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier =CellIdentifer;
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    NSDictionary* dic = [self.dataArray objectAtIndex:indexPath.row];
    
    WYView* mView = (WYView*)[cell viewWithTag:200];
    [mView setUpWithDict:dic];
    
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}


#pragma mark - UIScrollView methods
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   
    float offX = scrollView.contentOffsetLeft;
    int index = floorf(offX/self.view.width);
    NSLog(@"index = %d",index);
    if(index<0)
    {
        index = 0;
    }
    if(index>=self.dataArray.count)
    {
        index = self.dataArray.count - 1;
    }
    if(index!=self.currentItemIndex)
    {
        NSDictionary* dic = [self.dataArray objectAtIndex:index];
        self.titleLabel.text = [dic objectForKey:@"title"];
    }
    self.currentItemIndex = index;
    
}

#pragma mark - 生成collectionview和布局布局管理器
-(UICollectionView*)drawCollectionViewWithFrame:(CGRect)frame andFlowLayout:(CollectionFixedHeaderLayout*)flowLayout
{
    UICollectionView* myCollectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
   
    myCollectionView.showsVerticalScrollIndicator = NO;
    myCollectionView.showsHorizontalScrollIndicator = NO;
    myCollectionView.dataSource = self;
    myCollectionView.delegate = self;
    myCollectionView.pagingEnabled = YES;
    myCollectionView.backgroundColor = [UIColor clearColor];
    
    return myCollectionView;
}

-(void)registerCellAndHeaderForCollectionView:(UICollectionView*)collectionView {
    UINib *cellNib = nil;
    cellNib = [UINib nibWithNibName:@"WYCollectionCell" bundle:nil];
    [collectionView registerNib:cellNib forCellWithReuseIdentifier:CellIdentifer];
    
}

//一个布局管理器只能对应一个collectionView
-(CollectionFixedHeaderLayout*)getFlowLayout
{
    CollectionFixedHeaderLayout *flowLayout = [[CollectionFixedHeaderLayout alloc] init];
    flowLayout.isFiexed = NO;
    [flowLayout setItemSize:CGSizeMake(320, self.view.height-self.navTopContentView.bottom)];
    flowLayout.minimumInteritemSpacing =0;
    flowLayout.minimumLineSpacing = 0;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    return flowLayout;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
   
}


#pragma mark - xib按钮点击事件

- (IBAction)goBack:(id)sender {
    [kApp hideWaitView];
    [[AFGetData sharedInstance] removeAllGetAndPostRequest];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)goToShare:(id)sender {
    [kApp initShareViewWithUrl:ShareUrl andWithTitle:ShareTitle andWithInfo:NotifyRecommendShare andWithImageUlr:ShareImageUrl andIsSuccess:^(BOOL isSuccess) {
        if(isSuccess)
        {
            [PopTips tips:kApp.rootNav.view Msg:NotifyShareSuccess];
        }
        else{
            [PopTips tips:kApp.rootNav.view Msg:NotifyShareError];
        }
    }];

}
@end
